package com.supermarket.business.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.supermarket.business.domain.SupermarketInventoryChange;
import com.supermarket.business.domain.SupermarketProduct;
import com.supermarket.business.domain.SupermarketWarehouseManager;
import com.supermarket.business.mapper.SupermarketProductMapper;
import com.supermarket.business.mapper.SupermarketWarehouseManagerMapper;
import com.supermarket.business.vo.SupermarketInventoryChangeVO;
import com.supermarket.common.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.supermarket.business.mapper.SupermarketInventoryChangeMapper;
import com.supermarket.business.service.ISupermarketInventoryChangeService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 库存变动Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-21
 */
@Service
public class SupermarketInventoryChangeServiceImpl extends ServiceImpl<SupermarketInventoryChangeMapper, SupermarketInventoryChange> implements ISupermarketInventoryChangeService
{
    @Resource
    private SupermarketInventoryChangeMapper supermarketInventoryChangeMapper;
    @Resource
    private SupermarketWarehouseManagerMapper supermarketWarehouseManagerMapper;
    @Resource
    private SupermarketProductMapper supermarketProductMapper;

    /**
     * 查询库存变动
     * 
     * @param changeId 库存变动主键
     * @return 库存变动
     */
    @Override
    public SupermarketInventoryChange selectSysInventoryChangeByChangeId(Long changeId)
    {
        return supermarketInventoryChangeMapper.selectSysInventoryChangeByChangeId(changeId);
    }

    /**
     * 查询库存变动列表
     * 
     * @param supermarketInventoryChange 库存变动
     * @return 库存变动
     */
    @Override
    public List<SupermarketInventoryChange> selectSysInventoryChangeList(SupermarketInventoryChange supermarketInventoryChange)
    {
        List<SupermarketInventoryChange> list = supermarketInventoryChangeMapper.selectSysInventoryChangeList(supermarketInventoryChange);
        return list;
    }

    /**
     * 新增库存变动
     * 
     * @param supermarketInventoryChange 库存变动
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysInventoryChange(SupermarketInventoryChange supermarketInventoryChange)
    {
        SupermarketProduct product = supermarketProductMapper.selectSysProductByProductId(Long.valueOf(supermarketInventoryChange.getManagerId()));
        supermarketInventoryChange.setProductId(product.getProductId());
        supermarketInventoryChange.setManagerId(product.getProductName());
        return supermarketInventoryChangeMapper.insertSysInventoryChange(supermarketInventoryChange);
    }

    /**
     * 修改库存变动
     * 
     * @param supermarketInventoryChange 库存变动
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSysInventoryChange(SupermarketInventoryChange supermarketInventoryChange)
    {
        if(supermarketInventoryChange.getProductId() == null){
            SupermarketProduct product = supermarketProductMapper.selectSysProductByProductId(Long.valueOf(supermarketInventoryChange.getManagerId()));
            supermarketInventoryChange.setProductId(product.getProductId());
            supermarketInventoryChange.setManagerId(product.getProductName());
        }
        SupermarketProduct product = supermarketProductMapper.selectSysProductByProductId(supermarketInventoryChange.getProductId());
        if(!Objects.equals(product.getProductName(),supermarketInventoryChange.getManagerId())){
            SupermarketProduct product1 = supermarketProductMapper.selectSysProductByProductId(Long.valueOf(supermarketInventoryChange.getManagerId()));
            supermarketInventoryChange.setProductId(product1.getProductId());
            supermarketInventoryChange.setManagerId(product1.getProductName());
        }
        return supermarketInventoryChangeMapper.updateSysInventoryChange(supermarketInventoryChange);
    }

    /**
     * 批量删除库存变动
     * 
     * @param changeIds 需要删除的库存变动主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysInventoryChangeByChangeIds(Long[] changeIds)
    {
        return supermarketInventoryChangeMapper.deleteSysInventoryChangeByChangeIds(changeIds);
    }

    /**
     * 删除库存变动信息
     * 
     * @param changeId 库存变动主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysInventoryChangeByChangeId(Long changeId)
    {
        return supermarketInventoryChangeMapper.deleteSysInventoryChangeByChangeId(changeId);
    }

    /**
     * 获取库存变动VO列表
     * @param supermarketInventoryChange
     * @return
     */
    @Override
    public List<SupermarketInventoryChangeVO> selectSysInventoryChangeListVO(SupermarketInventoryChange supermarketInventoryChange) {
        List<SupermarketInventoryChange> list = supermarketInventoryChangeMapper.selectSysInventoryChangeList(supermarketInventoryChange);
        List<SupermarketInventoryChangeVO> listVO = new ArrayList<>();
        for (SupermarketInventoryChange change : list) {
            SupermarketInventoryChangeVO vo = new SupermarketInventoryChangeVO();
            BeanUtils.copyProperties(change, vo);
            listVO.add(vo);
        }
        return listVO;
    }

    /**
     * 获取库存变动VO详细信息
     * @param changeId
     * @return
     */
    @Override
    public SupermarketInventoryChangeVO selectSysInventoryChangeVOByChangeId(Long changeId) {
        SupermarketInventoryChange change = supermarketInventoryChangeMapper.selectSysInventoryChangeByChangeId(changeId);
        if (change == null) {
            throw new IllegalArgumentException("库存变动不存在");
        }
        SupermarketInventoryChangeVO vo = new SupermarketInventoryChangeVO();
        BeanUtils.copyProperties(change, vo);
        return vo;
    }

    private void CheckProductId(Long productId) {
        SupermarketProduct product = supermarketProductMapper.selectSysProductByProductId(productId);
        if (product == null) {
            throw new IllegalArgumentException("该商品Id不存在");
        }
    }

    private void CheckWarehouseManagerSysId(Long warehouseManagerSysId) {
        SupermarketWarehouseManager warehouseManager = new  SupermarketWarehouseManager();
        warehouseManager.setSysId(warehouseManagerSysId);
        List<SupermarketWarehouseManager> list = supermarketWarehouseManagerMapper.selectSysWarehouseManagerList(warehouseManager);
        if (list == null || list.isEmpty()) {
            throw new IllegalArgumentException("该仓库管理员账户id不存在");
        }
    }
}
